import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
// flutter向js传递数据，通过URL

const url = 'https://www.geekailab.com/io/flutter-trip/Flutter2JSByUrl.html?name=geekcc';

class Flutter2jsByUrl extends StatefulWidget {
  const Flutter2jsByUrl({super.key});

  @override
  State<Flutter2jsByUrl> createState() => _Flutter2jsByUrlState();
}

class _Flutter2jsByUrlState extends State<Flutter2jsByUrl> {
  int progress = 0;
  late WebViewController controller;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('flutter通过url向js传递数据'),
        actions: [_loadBtn],
      ),
      body: Stack(
        children: [
          WebViewWidget(controller: controller),
          Positioned(child: Text('加载进度：$progress'),bottom: 100,left: 100)
        ],
      ),
    );
  }

  get _loadBtn => FilledButton(onPressed: (){
    controller.loadRequest(Uri.parse(url));
  },child: const Text('加载H5页面',style: TextStyle(color: Colors.white)));

  @override
  void initState() {
    super.initState();
    controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..setNavigationDelegate(NavigationDelegate(onProgress: (progress){
      setState(() {
        this.progress = progress;
      });
    }))
    ;
  }
}
